package com.example.qxfw.tourqx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.qxfw.common.entity.zy.TourFcst;
import com.example.qxfw.common.util.DateUtil;
import com.example.qxfw.tourqx.service.JdybService;
import com.example.qxfw.ybzz.mapper.TourFcstMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
public class JdybServiceImpl implements JdybService {
    @Autowired
    private TourFcstMapper tourFcstMapper;

    @Override
    public List<TourFcst> getAllOfTourFcst(String start, String end, int forecasttype) {
        Timestamp startTime = DateUtil.localDateTimeToTimestamp(DateUtil.stringToLocalDateTime(start));
        Timestamp endTime = DateUtil.localDateTimeToTimestamp(DateUtil.stringToLocalDateTime(end));
        return tourFcstMapper.selectList(new LambdaQueryWrapper<TourFcst>()
                .eq(TourFcst::getForecasttype, forecasttype)
                .between(TourFcst::getDatechar, startTime, endTime));
    }

    @Override
    public int updateByNitmeAndStationName(TourFcst tourFcst) {
        Integer ntimes = tourFcst.getNtimes();
        tourFcst.setNtimes(null);
        String stationname = tourFcst.getStationname();
        tourFcst.setStationname(null);
        String timechar = tourFcst.getTimechar();
        tourFcst.setTimechar(null);
        Integer forecasttype = tourFcst.getForecasttype();
        tourFcst.setForecasttype(null);
        String stationid = tourFcst.getStationid();
        tourFcst.setStationid(null);
        Date datechar = tourFcst.getDatechar();
        tourFcst.setDatechar(null);

        UpdateWrapper<TourFcst> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("ntimes", ntimes)
                .eq("stationname", stationname)
                .eq("timechar", timechar)
                .eq("forecasttype", forecasttype)
                .eq("stationid", stationid)
                .eq("datechar", datechar);

        return tourFcstMapper.update(tourFcst, updateWrapper);
    }


    @Override
    public int deleteByNitmeAndStationName(String stationname, String timechar, int ntimes, int forecasttype) {
        return tourFcstMapper.delete(new QueryWrapper<TourFcst>()
                .eq("ntimes", ntimes)
                .eq("stationname", stationname)
                .eq("timechar", timechar)
                .eq("forecasttype", forecasttype));

    }
}
